<?php //$Rev:: 55         $ // DO NOT REMOVE

/**
 *
 *
 *
 * Testing big block comments
 * Hopefully the tokenizer works with them
 *
 *
 *
 *
 *
 */
function tweet_updateTwitter(&$obj, $data)
{
    // !tweet <user> <pass> <message>

    // this line ensures that the message text is not saved to our DB (privacy policy)
    //   now username and password are passed through, and never saved  ;)
    $obj->core->parser->ignoreAlert('tweet');

    if ($text = $obj->core->module->aim_help_requested($data)) {
        $obj->core->utilities->aim_send_im($data['screenname'], $text, true);
    } else {
        @list($username, $password, $message) = explode(' ', $data['message_text'], 3);
        // shorten the message if needed...
        if (strlen($message) > 140) $message = substr($message, 0, 137) . '...';

        // REQUIRES PHP 5.3.0
        $twitterReq = function() use ($username, $password, $message) {
            $context = stream_context_create(array(
                'http'      => array(
                    'method'  => 'POST',
                    'header'  => sprintf("Authorization: Basic %s\r\n", base64_encode($username . ':' . $password)) . "Content-type: application/x-www-form-urlencoded\r\n",
                    'content' => http_build_query(array('status' => $message)),
                    'timeout' => 5
                )
            ));
            $ret = file_get_contents('http://twitter.com/statuses/update.xml', false, $context);
        
            return ($ret !== false) ? 
                'Twitter status updated for <b>' . $username . '</b>.  Please close this IM windows so nobody sees your user information.' :
                'Twitter reported an error.  Possibly your username/password?';
        };

        if ($data['message_type'] == AIM_MSG_CHAT) {
            $obj->core->chat->aim_chat_send($data['cid'], $twitterReq() . '  Perhaps you should think twice about posting your username/password in public?');
        } else {
            $obj->core->utilities->aim_send_im($data['screenname'], $twitterReq(), true);
        }
    }
}

function tweetget_getUserTweet(&$obj, $data)
{
    if ($text = $obj->core->module->aim_help_requested($data)) {
        $obj->core->utilities->aim_send_im($data['screenname'], $text, true);
    } else {
        $tweetGet = function() use ($data) {
            $id = trim($data['message_text']);
            $twitter_feed = 'http://twitter.com/statuses/user_timeline.xml?screen_name=' . $id;    
            $ctx = stream_context_create(array('http' => array('timeout' => 1)));
            $rawfeed = @file_get_contents($twitter_feed, 0, $ctx);

            if ($rawfeed !== false) {
                $xml = new SimpleXmlElement($rawfeed);
                $tweet = utf8_decode($xml->status->text);
                $tweet = preg_replace('@(https?://([-\w\.]+)+(d+)?(/([\w/_\.]*(\?\S+)?)?)?)@', '<a href="$1">$1</a>',  $tweet );
                $tweet = preg_replace("#(^|[\n ])@([^ \"\t\n\r<]*)#ise", "'\\1<a href=\"http://www.twitter.com/\\2\" >@\\2</a>'", $tweet);  
                $tweet = preg_replace("#(^|[\n ])\#([^ \"\t\n\r<]*)#ise", "'\\1<a href=\"http://twitter.com/search?q=%23\\2\" >#\\2</a>'", $tweet);
        
                // format the time
                $time = strtotime(str_replace("+0000", "", $xml->status->created_at . " UTC"));
                $delta = time() - $time;
                if ($delta < 60) {
                    $date = 'less than a minute ago';
                } elseif ($delta < 120) {
                    $date = 'about a minute ago';
                } elseif ($delta < (45 * 60)) {
                    $date = floor($delta / 60) . ' minutes ago';
                } elseif ($delta < (90 * 60)) {
                    $date = 'about an hour ago';
                } elseif ($delta < (24 * 60 * 60)) {
                    $date = 'about ' . floor($delta / 3600) . ' hours ago';
                } elseif ($delta < (48 * 60 * 60)) {
                    $date = '1 day ago';
                } else {
                    $date = floor($delta / 86400) . ' days ago';
                }
                return 'Posted by <b>' . $id . '</b> (' . $date . '): "' . str_replace($id . ': ','', $tweet) . '"';
            } else {
                return 'Couldn\'t get Twitter status for ' . $id . '.';
            }
        };

        if ($data['message_type'] == AIM_MSG_CHAT) {
            $obj->core->chat->aim_chat_send($data['cid'], $tweetGet());
        } else {
            $obj->core->utilities->aim_send_im($data['screenname'], $tweetGet(), true);
        }
    }
}

?>